package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.AccountProxy;
	import com.rogo.rogobox.views.components.ContentTop;
	import com.rogo.rogobox.views.components.consts.ContentMiddleConst;
	import com.rogo.rogobox.views.components.consts.PageMainConst;
	import com.sina.microblog.data.MicroBlogUser;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	public class ContentTopMediator extends Mediator
	{
		public function ContentTopMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'contentTopMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var user:MicroBlogUser;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.USER_PROFILE_RESULT];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			user = notification.param as MicroBlogUser;
			
			updateDisplay();
		}
		
		override public function onRegister():void
		{
			view.userImg.addEventListener(MouseEvent.CLICK, userImg_clickHandler);
			view.friendsLab.addEventListener(MouseEvent.CLICK, friends_clickHandler);
			view.followsLab.addEventListener(MouseEvent.CLICK, followsLab_clickHandler);
			view.msgsLab.addEventListener(MouseEvent.CLICK, msgsLab_clickHandler);
			
			var accountProxy:AccountProxy = facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
			user = accountProxy.user;
			
			updateDisplay();
			
			// create context menu
			createContextMenu();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():ContentTop
		{
			return viewComponent as ContentTop;
		}
		
		private function updateDisplay():void
		{
			if (!user)
				return;
			
			view.userImg.source = user.profileImageUrl;
			view.userName.text = user.name;
			view.msgsLab.text = '微博' + user.statusesCount.toString();
			view.followsLab.text = '粉丝' + user.followersCount.toString();
			view.friendsLab.text = '关注' + user.friendsCount.toString();
		}
		
		private function createContextMenu():void
		{
			var contextMenu:ContextMenu = new ContextMenu();
			var refreshMenuItem:ContextMenuItem = new ContextMenuItem('刷新');
			refreshMenuItem.addEventListener(Event.SELECT, refreshMenuItem_selectHandler);
			contextMenu.customItems = [refreshMenuItem];
			view.contextMenu = contextMenu;
		}
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLERS
		//
		//--------------------------------------------------------------------------
		
		private function refreshMenuItem_selectHandler(e:Event):void
		{
			sendNotification(AppFacade.REFRESH_USER_PROFILE, user.id);
		}
		
		private function userImg_clickHandler(e:MouseEvent):void
		{
			App.getInstance().profilePageUser = App.getInstance().verifyUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_PROFILE);
			sendNotification(AppFacade.DISPLAY_USER_PROFILE);
		}
		
		private function friends_clickHandler(e:MouseEvent):void
		{
			App.getInstance().friendPageIsFollower = false;
			App.getInstance().friendPageUser = App.getInstance().verifyUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_FRIEND);
			sendNotification(AppFacade.DISPLAY_USER_FRIENDS);
		}
		
		private function followsLab_clickHandler(e:MouseEvent):void
		{
			App.getInstance().friendPageIsFollower = true;
			App.getInstance().friendPageUser = App.getInstance().verifyUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_FRIEND);
			sendNotification(AppFacade.DISPLAY_USER_FOLLOWER);
		}
		
		private function msgsLab_clickHandler(e:MouseEvent):void
		{
			App.getInstance().mainPageDisplay = PageMainConst.DISPLAY_MAIN;
			App.getInstance().mainPageUser = App.getInstance().verifyUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_MAIN);
			sendNotification(AppFacade.DISPLAY_USER_MAIN);
		}
		
	}
}